{
	var redir=getParameter("redir");
	if(typeof(redir)!='undefined'&&redir!=null&&redir.length>0){
		var code=getParameter("code");
		var state=getParameter("state");
		window.location.href=redir+"?code="+code+"&state="+state;
		
	}else{
		var state=getParameter("state");
		if(state!=null&&typeof(state)!='undefined'&&state=='sina'){
			var code=getParameter("code");
			jQ.ajax({
				type: "POST",
				url:"userAjax!checkSinacode.action",
				data:{
					code:code
				},
				async:false,
				success:function(data){
					if(data.result){
						if(data.loginstatus){
							if(typeof(data.fwurl)!='undefined'&&data.fwurl.length>0){
								window.location.href=data.fwurl;
							}else{
								if(data.usertype==2)
									window.location.href=window.location.host+"/myclip/calendar.html";
								else
									window.location.href=window.location.host+"/myclip/home.html";
							}
						}else{
							window.location.href=window.location.host+"/register.html?state=sina";
						}
					}else{
						alert(data.errormsg);
					}
				}
			});
		}
	}
}

jQ(function(){

	jQ("#pwdipt").keydown(function(event){
		if(event.keyCode==13){
			signIn();
		}
	});
	jQ("#pwdipt").val("");
	jQ("#emailipt").val("");
	jQ("#emailipt").focus();
	jQ("#ALERT").dialog({
		modal: true,
		bgiframe: true,
		autoOpen: false,
		resizable:false,
		closeOnEscape:false,
		draggable:true
	});
	jQ.ajax({
		type: "POST",
		url:"userAjax!checkExistUser.action",
		success:function(data){
			if(data!=null){
				var dataObj=eval("("+data+")");
				if(dataObj['loginflag'])
					if(dataObj.usertype==2)
						window.location.href=jQ("#ctxVal").val()+"/myclip/calendar.html";
					else
						window.location.href=jQ("#ctxVal").val()+"/myclip/home.html";
			}
		}
	});
	initLogin();
	initLastSubscribeContent();
	jQ("#cliprecommend li").mouseover(function(){
		if(jQ(this).hasClass("on"))
			return;
		jQ(this).addClass("focus");
	});
	jQ("#cliprecommend li").mouseout(function(){
		if(jQ(this).hasClass("on"))
			return;
		jQ(this).removeClass("focus");
	});
	jQ("#cliprecommend li").click(function(){
		if(jQ(this).hasClass("on"))
			return;
		jQ("#cliprecommend li.on").removeClass();
		jQ(this).removeClass().addClass("on");
	});
});

function initLogin(){
	jQ("#user_errorinfo").text(" ");
	jQ("#userlogin_form").validate({
		rules:{   
			username:{   
		        required:true
		    },
		    password:{
		    	required:true
		    }
		},
		messages:{
			username: {  
				required: "请输入账号名称"
			},
			password: {  
				required: "请输入密码"
			}
		},
		errorPlacement: function(error,element)     // 将错误提示信息放在什么地方     
		{
			error.appendTo(jQ("#user_errorinfo"));
		},
        submitHandler: function(form) {
        	signIn();
        	return false;
        },
        focusInvalid: false,   
        onkeyup: false  
	});
}

function initLastSubscribeContent(){
	
	jQ.ajax({
		type: "POST",
		url: "eventAjax!loadLastestSubscribeClips.action",
		success:function(data){
			if(data.clips.length>0){
				var html="";
				for(var i=0;i<data.clips.length;i++){
					var clipobj=data.clips[i];
					var eventid=clipobj.eventid;
					var title=clipobj.title;
					var name=clipobj.name;
					var uid=clipobj.uid;
					var biglogo=clipobj.biglogo;
					var smalllogo=clipobj.smalllogo;
					var iswholeday=eval(clipobj.iswholeday);
					var starttime=eval(clipobj.starttime);
					var endtime=eval(clipobj.endtime);
					var content=clipobj.content;
					var clipeventpic=clipobj.clipeventpic;
					var favorcount=clipobj.favorcount;
					var shareid=clipobj.shareid;
					html+="<tr>";
						html+="<td class=\"avatar\">";
							html+="<div class=\"logo\" style=\"background:url('"+smalllogo+"')\" title='"+name+"'>" +
									"<a href='"+jQ("#ctxVal").val()+"/myclip/user.html?"+uid+"'></a>" +
									"</div>";
							html+="<a class=\"ssbtn\" uid="+uid+"></a>";
						html+="</td>";
						html+="<td class=\"theme\">";
						html+="<div class=\"ssname\"><a href='"+jQ("#ctxVal").val()+"/myclip/user.html?"+uid+"'>"+name+"</a></div>";
						html+="<div class=\"time\">";
							html+="<span class=\"a\">活动时间：</span>";
							if(iswholeday){
								html+="<span>开始于&nbsp;</span>";
								html+="<b>"+formateDetailTime(starttime)+"</b>";
							}else{
								html+="<b>"+formateDetailTime(starttime)+"</b>";
								html+="<span>&nbsp;至&nbsp;</span>";
								html+="<b>"+formateDetailTime(endtime)+"</b>";
							}
						html+="</div>";
						html+="<div class=\"topic\">";
							html+="<span class=\"a\">内容：</span>";
							html+="<span class=\"name\">"+title+"</a>";
						html+="</div>";
						if((content!=null&&content.length>0)||(clipeventpic!=null&&clipeventpic.length>0)){
							html+="<div class=\"txt\">";
							html+="<span class=\"a\">详情：</span>";
							if(content!=null&&content.length>0){
								html+="<span class=\"val\">";
								html+=content;
								html+="</span>";
							}
							if(clipeventpic!=null&&clipeventpic.length>0){
								html+="<div class=\"picture\">";
									html+="<img src='"+jQ("#ctxVal").val()+"/clippic/S_"+clipeventpic+"'>";
								html+="</div>";
							}
							html+="</div>";
						}
							
						html+="</td>";
						html+="<td class=\"like\">";
							html+="<div class=\"icon\">( "+favorcount+" )</div>";
							html+="<a class=\"detail\" target='_blank' href='"+jQ("#ctxVal").val()+"/myclip/clip_detail.html?"+shareid+"'></a>";
						html+="</td>";
					html+="</tr>";
				}
				jQ("#clip_content").empty().append(html);
				jQ("#clip_content tr td.avatar").find("a.ssbtn").unbind("click").bind("click",subscribecc);
			}
		}
	});
}

function subscribecc(event){
	var obj=jQ(event.currentTarget);
	if(obj.hasClass("alss"))
		return;
	
	var uid=obj.attr("uid");
	jQ("#useremail_form").data("uid",uid);
	var obj_offset = jQ(this).offset();
	var _top = obj_offset.top - jQ("#user_emailinput").outerHeight()*2 ;
	var _left = obj_offset.left - jQ("#user_emailinput").outerWidth()/4;
	jQ("#user_emailinput_val").val("");
	jQ("#user_emailinput").css({"top":_top+"px","left":_left+"px"}).show();
	jQ("#user_emailinput_errorinfo").text(" ");
	jQ("#useremail_form").validate({
		rules:{   
			emailinput:{   
				required:true,
				email:true
			}   
		},
		messages:{
			emailinput: {  
				required: "请输入邮件地址",
				email: "请输入正确格式的电子邮件"
			}
		},
		errorPlacement: function(error,element)     // 将错误提示信息放在什么地方     
		{
			error.appendTo(jQ("#user_emailinput_errorinfo"));
		},
		submitHandler: function(form) {
			jQ("#loadingui").show();
			var uid=jQ("#useremail_form").data("uid");
			var val=jQ.trim(jQ("#user_emailinput_val").val());
			ajaxForSubscribecc(uid,val,obj);
		},
		focusInvalid: false,   
		onkeyup: false  
	});
}

function ajaxForSubscribecc(uid,email,btnobj){
	jQ.ajax({
		type: "POST",
		url: "contactAjax!subscribeCC.action",
		data:{
			uid : uid,
			subscribeemail : email
		},
		success:function(data){
			if(data!=null){
				var dataObj=eval("("+data+")");
				if(dataObj.result){
					btnobj.addClass("alss").unbind("click");
					jQ("#user_emailinput").hide();
					jQ("#loadingui").hide();
				}
			}
		}
	});
}

function InitBackGroundPic(){
	var abc=parseInt(Math.random()*3+1);
	jQ("#about_reminder").empty().append("<img src='image/clip24-"+abc+".jpg' width='878'/>");
}
//Jquery的弹出框
function jalert(info,LFun){
	jQ("#ALERT p").text(info);
	if(LFun!=null)jQ("#ALERT").bind("LFun",LFun);
	jQ("#ALERT").parent().css('font', "14px \"Trebuchet MS\", sans-serif");
	jQ("#ALERT").dialog('open');
	jQ("#ALERT").dialog({buttons:{"确定":function(){
		jQ(this).dialog('close');
	}}});
}

function resendActiveEmail(){
	var username = jQ("#emailipt").val();
	var password = jQ("#pwdipt").val();
	jQ("#ALERT").dialog({
		buttons:[{
			text : "确定",
			click: function() {
				jQ(this).dialog("close");
			}
			}
		]
	});
	jQ.ajax({
		type: "POST",
		url: "userAjax!resendActiveEmail.action",
		data:{
			emailInput : username,
			pwdInput : password
		},
		success:function(data){
			var dataObj=data;
			if(dataObj.result){
				jQ("#ALERT p").empty().append("激活邮件发送成功，请尽快登录邮箱激活。");
			}else{
				jQ("#ALERT p").empty().append(dataObj.errormsg);
			}
			jQ("#ALERT").parent().css('font', "14px \"Trebuchet MS\", sans-serif");
			jQ("#ALERT").dialog('open');
		}
	});
}

function signIn(){
	var username = jQ("#emailipt").val();
	var password = jQ("#pwdipt").val();
	var autochecked = ""+jQ("#autocbx").attr("checked");
	var p = /^[a-zA-Z0-9][a-zA-Z0-9_-]{5,19}$/;
//	if(!isEmail(username)){
//		var ccpos=username.lastIndexOf(".clip");
//		var ccpos2=username.lastIndexOf(".test");
//		if(ccpos>0&&ccpos==username.length-5){
//			
//		}else if(ccpos2>0&&ccpos2==username.length-5){
//			
//		}else{
//			jalert("账号错误,请重新输入");
//			return;
//		}
//	}
	if(!p.test(password)){
		jalert("密码错误,请重新输入");
		return;
	}
	jQ.ajax({
		type: "POST",
		url: "userAjax!login.action",
		data:{
			emailInput : username,
			pwdInput : password,
			activate : autochecked
		},
		success:function(data){
			var dataObj=data;
			if(dataObj.result){
				var userstatus=dataObj.userstatus;
				var usertype=dataObj.usertype;
				if(userstatus==0){
					jQ("#ALERT").dialog({
						buttons:[{
							text : "确定",
							click: function() {
								resendActiveEmail();
							}
						},
						{
							text : "取消",
							click: function() {jQ(this).dialog("close");}
						}
						]
					});
					jQ("#ALERT p").empty().append("您的账号还未激活，是否需要再次发送激活邮件？");
					jQ("#ALERT").parent().css('font', "14px \"Trebuchet MS\", sans-serif");
					jQ("#ALERT").dialog('open');
				}else if(userstatus==2){
					jalert("您的账号被禁止登录");
				}else{
					var url=dataObj.visiturl;
					if(url!=null&&url.length>0){
						jQ("#logininfozone").empty().append("登录成功，跳转到您之前的页面。");
						setTimeout(function(){
							window.location.href=url;
						},2000);
					}else
						if(usertype==2)
							window.location.href=jQ("#ctxVal").val()+"/myclip/calendar.html";
						else
							window.location.href=jQ("#ctxVal").val()+"/myclip/home.html";
				}
			}else{
				jalert(dataObj.errormsg);
			}
		}
	});
}

function formateDetailTime(daytime){
	var temp=new Date();
	temp.setTime(eval(daytime));
	return temp.pattern("yyyy-MM-dd HH:mm");
}

function gotosina(){
//	var url=window.location.href;
	jQ.ajax({
		type: "POST",
		url: "userAjax!createSinaOAuth2URL.action",
		data:{
			suffixurl:""
		},
		success:function(data){
			if(data.result){
				if(data.status){
					window.location.href=jQ("#ctxVal").val()+"/myclip/home.html";
				}else{
					window.location.href=data.oauthurl;
				}
			}else{
				clip_tips(data.errormsg);
			}
		}
	});
}

function getParameter(name){
    var paramStr=location.search;
    if(paramStr.length==0)return null;
    if(paramStr.charAt(0)!='?')return null;
    paramStr=unescape(paramStr);
    paramStr=paramStr.substring(1);
    if(paramStr.length==0)return null;
    var params=paramStr.split('&');
    var p = null;
    for(var i=0;i<params.length;i++){
        if(params[i].indexOf(name) >= 0){
            p = params[i].split('=');
            if(p[0]==jQ.trim(name))
            	p = p[1];
        }
    }
    return p;
}